package jin.si.com.web;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @description:
 * @ClassName TestWorkboot
 * @Author MT
 * @Date 2021/5/29 10:04
 **/
public class TestWorkboot {

    public static void main(String[] args) {
        /*try {
            //1：创建输出流
            FileOutputStream os=new FileOutputStream("d:/workboot/workboot.xls");
            //2：创建工作部
            Workbook wk=new HSSFWorkbook();
            //3：创建sheet
            Sheet sheet = wk.createSheet();
            //4：创建行
            Row row = sheet.createRow(0);
            //5：创建列
            Cell cell = row.createCell(0);
            //6：设置单元格内容
            cell.setCellValue("你好");
            //7：输出
            ((HSSFWorkbook) wk).write(os);
            //8：关闭
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            FileOutputStream os=new FileOutputStream("d:/workboot/workboot2.xls");
            Workbook wb=new HSSFWorkbook();
            Sheet sheet = wb.createSheet();
            for(int a=0;a<65536;a++){
                Row row = sheet.createRow(a);
                for(int b=0;b<255;b++){
                    Cell cell = row.createCell(b);
                    cell.setCellValue(Math.ceil(Math.random()*1000));
                }
            }
           ((HSSFWorkbook) wb).write(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }*/


        //xlsx
        /*try {
            FileOutputStream os=new FileOutputStream("d:/workboot/workboot3.xlsx");
            SXSSFWorkbook wb=new SXSSFWorkbook(10000); //-1关闭时候刷新
            SXSSFSheet sheet = wb.createSheet();
            for(int rowNum=0;rowNum<10000;rowNum++){
                SXSSFRow row = sheet.createRow(rowNum);
                for(int cellNum=0;cellNum<10;cellNum++){

                    SXSSFCell cell = row.createCell(cellNum);
                    cell.setCellValue(Math.ceil(Math.random()*100));
                }
                //每100行刷新一次 防止内存溢出 和  SXSSFWorkbook wb=new SXSSFWorkbook(100)等价
                if(rowNum%100==0){
                    sheet.flushRows();
                }
            }
            SXSSFSheet sheet1 = wb.createSheet();
            for(int rowNum=0;rowNum<10000;rowNum++){
                SXSSFRow row = sheet1.createRow(rowNum);
                for(int cellNum=0;cellNum<10;cellNum++){
                    SXSSFCell cell = row.createCell(cellNum);
                    cell.setCellValue(Math.ceil(Math.random()*100));
                }

            }
            wb.write(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }*/


        /*//读取xls
        try {
            //1：创建输入流
            FileInputStream is=new FileInputStream("d:/workboot.xls");
            //2：创建工作簿
            HSSFWorkbook wb=new HSSFWorkbook(is);
            //3：获取getSheetAt
            HSSFSheet sheetAt = wb.getSheetAt(0);
            //4：获取行
            for(int rowNum=0;rowNum<=sheetAt.getLastRowNum();rowNum++){
                HSSFRow row = sheetAt.getRow(rowNum);
                //5：获取列
                for(int cellNum=0;cellNum<row.getLastCellNum();cellNum++){
                    HSSFCell cell = row.getCell(cellNum);
                    String str = cell.getStringCellValue();
                    System.out.println(str);
                }
            }
            //6：关闭
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        //读取xlsx
        try {
            FileInputStream is=new FileInputStream("d:/workboot/workboot.xlsx");
            XSSFWorkbook wb=new XSSFWorkbook(is);
            XSSFSheet sheetAt = wb.getSheetAt(0);
            for(int rowNum=0;rowNum<= sheetAt.getLastRowNum();rowNum++){
                XSSFRow row = sheetAt.getRow(rowNum);
                for(int cellNum=0;cellNum<row.getLastCellNum();cellNum++){
                    XSSFCell cell = row.getCell(cellNum);
                    String str = cell.getStringCellValue();
                    System.out.println(str+"*************************");
                }
            }
        is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
